#include "tictacheader.h"

char CheckWin(const char board[][NUM_COLS])
{
	//used for return of the winner or 0 to continue playing
	char winner;
	winner = '0';

	//index of what row or column should be checked
	int index;
	index = 0;

	//Check rows and columns
	while(index < NUM_COLS && winner == '0')
	{
		//check rows
		if(board[index][0] != ' ' && board[index][0] == board[index][1]
		                          && board[index][0] == board[index][2])
		{
			winner = board[index][0];
		}

		//check columns
		if(board[0][index] != ' ' && board[0][index] == board[1][index]
		                          && board[0][index] == board[2][index])
		{
			winner = board[0][index];
		}

		index++;
	}

	//Check diagonals - skips checking a diagonal if a winner is found
	if(board[1][1] != ' ' && winner == '0')
	{
		if(board[0][0] == board[1][1] && board[1][1] == board[2][2])
		{
			winner = board[1][1];
		}

		if(board[2][0] == board[1][1] && board[1][1] == board[0][2])
		{
			winner = board[1][1];
		}
	}

	//if 3 in a row are found, the letter of the winner is returned, else 0
	return winner;
}
